﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.DataContract;
using CommonsLib.DB;
using MaxLeaf.Platform.DALImplementation;
using MaxLeaf.Platform.Functions;
using CommonsLib.Utility;
using MaxLeaf.Platform.Commons;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;
using CommonsLib.Utility;
using CommonsLib.Settings;

namespace MaxLeaf.Platform.BLLImplementation
{
    public class poOrderRecvHdrBLL : BaseBLL
    {
       
        public override string FinterIDFields
        {
            get
            {
                return string.Format("{0},{1}", "FInterID", (int)FinterIDType.poOrderRecvHdr);
            }
        }


        internal void SetPosted(DBAccess dbcon, int intHdrID)
        {
            ((poOrderRecvHdrDAL)this.CurrentDAL).SetPosted(dbcon, intHdrID);
        }

        public void WritePHPOrderToSCP(string strWebOrder_ID)
        {
            using (DBAccess dbSCPBase = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPBase))
                , dbWebRead = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_WebRead))
                , dbOrd = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPSEOrder))
                )
            {
                try
                {

                    poOrderRecvHdrBLL poHdrBLL = new poOrderRecvHdrBLL();
                    poOrderRecvHdrDataModel poHdrModel = new poOrderRecvHdrDataModel();
                    poHdrModel = (poOrderRecvHdrDataModel)poHdrBLL.InsertFinterID(poHdrModel);
                    {
                        dbOrd.BeginTrans();
                        if (((poOrderRecvHdrDAL)this.CurrentDAL).WritePHPOrderToSCPHdr(dbWebRead, dbOrd, dbSCPBase, poHdrModel, strWebOrder_ID))
                        {
                            poHdrModel = (poOrderRecvHdrDataModel)poHdrBLL.Update(poHdrModel);
                        }
                        else
                        {
                            return;
                        }
                        string strSQL = "select * from order_goods where order_ID = " + strWebOrder_ID + "";
                        using (DbDataReader drWebOrder = dbWebRead.GetDataReader(strSQL))
                        {
                            while (drWebOrder.Read())
                            {
                                poOrderRecvDtlBLL poDtlBLL = new poOrderRecvDtlBLL();
                                poOrderRecvDtlDataModel poDtlModel = new poOrderRecvDtlDataModel();

                                poDtlModel.CompanyId = poHdrModel.CompanyId;
                                poDtlModel.Cost = CvtFunc.ToDouble(drWebOrder["goods_price"]);
                                poDtlModel.ForeignKey = poHdrModel.Key;
                                strSQL = "SELECT B.unit_group_id FROM goods_measure_unit  B where  B.measure_unit_id='" + drWebOrder["order_unit_id"] + "'";
                                string strUnit_group_ID = dbSCPBase.GetValue(strSQL);
                                string strgoods_id = CvtFunc.ToString(drWebOrder["goods_id"]);
                                string strattr_id = CvtFunc.ToString(drWebOrder["goods_attr_id"]);

                                strSQL = @"  select distinct isnull(b.goods_attr_id,0)
                                    from dbo.goods_main a
                                    left join dbo.goods_attribute b on a.goods_id=b.goods_id
                                    left join dbo.goods_measure_unit c on a.unit_group_id=c.unit_group_id 
                                    where a.goods_id=@goods_id and b.attr_id=@attr_id and c.unit_group_id=@unit_group_id";


                                string strGoods_attr_id = dbSCPBase.GetValue(strSQL, new SqlParameter[]{ 
                                                       new SqlParameter("@goods_id",strgoods_id )
                                                    ,  new SqlParameter("@attr_id", strattr_id )
                                                    , new SqlParameter( "@unit_group_id" , strUnit_group_ID )});


                                poDtlModel.ItemId = CvtFunc.ToInt(dbSCPBase.GetValue("Select ItemID from iminvmaster where GoodsID=@GoodsID and GoodsAttrID=@GoodsAttrID",
                                                                  new SqlParameter[]{ new SqlParameter( "@GoodsID", strgoods_id )
                                                                              , new SqlParameter ("@GoodsAttrID", strGoods_attr_id)}));
                                poDtlModel.Qty = CvtFunc.ToDouble(drWebOrder["order_unit_number"]);
                                poDtlModel.UnitId = CvtFunc.ToInt(drWebOrder["order_unit_id"]);
                                poDtlModel.WebPrice = CvtFunc.ToDouble(drWebOrder["goods_price"]);
                                poDtlModel.SubTotal = CvtFunc.ToDouble(drWebOrder["goods_amount"]);
                                poDtlBLL.Add(poDtlModel);
                            }
                        }
                        dbOrd.CommitTrans();
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Debug(ex.Message);
                    dbOrd.RollbackTrans();
                }
            }
        }

        internal void SetUnPosted(DBAccess dbcon, int intHdrID)
        {
            ((poOrderRecvHdrDAL)this.CurrentDAL).SetUnPosted(dbcon, intHdrID);
        }
    }
}
